<?php
/**
 *description:包裹管理
 *author:李飞@天众科技
 *time:2016/12/27
 */

namespace Admin\Controller;
use Think\Controller;
class BagController extends CommonController {
	
    public function index(){
		$this->redirect('inbound');
    }
	public function inbound(){
		$t_no=serialNumber('ArrivalOrder');//流水号
		$order_no='BAG'.date('Ymd',time()).$t_no;//流水单号
		$m=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
		$storage_list=$m->select();
		
		$this->assign('storage_list',$storage_list);
		$this->assign('order_no',$order_no);
		$this->display();
    }
	public function outbound(){
		$this->display();
    }
	
	//包裹列表数据
	public function get_bagInbound(){
        $ck=A('CheckInput');
		//参数验证
        $order_no		=	$ck->in('单号','order_no','ennumstr','',0,0);
        $receiver_name	=	$ck->in('收件人姓名','receiver_name','cnennumstr','',0,0);
        $receiver_phone	=	$ck->in('收件人电话','receiver_phone','ennumstr','',0,0);
        $receiver_address	=	$ck->in('收件人地址','receiver_address','cnennumstr','',0,0);
		$sender_name	=	$ck->in('寄件人姓名','sender_name','cnennumstr','',0,0);
        $sender_phone	=	$ck->in('寄件人电话','sender_phone','ennumstr','',0,0);
        $sender_address	=	$ck->in('寄件人地址','sender_address','cnennumstr','',0,0);
		
		$page=$ck->in('当前页','page','intval','1',0,0);
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
		$status=I('status');
		//排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		//搜索条件
		$map=array();
		
		!empty($order_no)?$map['order_no']=array('like','%'.$order_no.'%'):'';
        !empty($receiver_name)?$map['receiver_name']=array('like','%'.$receiver_name.'%'):'';
		!empty($receiver_phone)?$map['receiver_phone']=array('like','%'.$receiver_phone.'%'):'';
		!empty($receiver_address)?$map['receiver_address']=array('like','%'.$receiver_address.'%'):'';
		!empty($sender_name)?$map['sender_name']=array('like','%'.$sender_name.'%'):'';
		!empty($sender_phone)?$map['sender_phone']=array('like','%'.$sender_phone.'%'):'';
		!empty($sender_address)?$map['sender_address']=array('like','%'.$sender_address.'%'):'';
		
		//获取数据
        $data=getTable('BagOrder',$map,$page,$rows,$sort,$order,$status);
		
        $this->ajaxReturn($data);
    }
	//添加入库包裹信息
    public function bagInboundAdd(){
		$m=M('BagOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput'); 
		$data['order_no']=I('order_no');
		$data['storage_code']=I('storage_code');
		$data['receiver_name']=I('receiver_name');
		$data['receiver_address']=I('receiver_address');
		$data['receiver_phone']=I('receiver_phone');
		$data['receiver_code']=I('receiver_code');
		$data['sender_name']=I('sender_name');
		$data['sender_address']=I('sender_address');
		$data['sender_phone']=I('sender_phone');
		$data['sender_code']=I('sender_code');
		$data['mail_no']=I('mail_no');
		$data['weight']=I('weight');
		$data['price']=I('price');
		$data['total_price']=I('total_price');
		$data['remark']=I('remark');
		$data['add_time']=time();
		$data['update_time']=time();
        $insertId=$m->add($data);
        if(!$insertId){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//更新入库包裹信息
    public function bagInboundSave(){
		$m=M('BagOrder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['order_no']=I('order_no');
		$data['storage_code']=I('storage_code');
		$data['receiver_name']=I('receiver_name');
		$data['receiver_address']=I('receiver_address');
		$data['receiver_phone']=I('receiver_phone');
		$data['receiver_code']=I('receiver_code');
		$data['sender_name']=I('sender_name');
		$data['sender_address']=I('sender_address');
		$data['sender_phone']=I('sender_phone');
		$data['sender_code']=I('sender_code');
		$data['mail_no']=I('mail_no');
		$data['weight']=I('weight');
		$data['price']=I('price');
		$data['total_price']=I('total_price');
		$data['remark']=I('remark');
		$data['update_time']=time();
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//////////////////////////////////////////////////////
	//审核
    public function examineBagInbound(){
		$return=tableExamine('BagOrder');
        $this->ajaxReturn($return);
    }
	//删除
    public function bagInboundDel(){
		$return=tableDel('BagOrder');
        $this->ajaxReturn($return);
    }
	//清空回收站
    public function bagInboundClear(){
		$return=tableClear('BagOrder');
        $this->ajaxReturn($return);
    }
	//恢复回收站数据
    public function bagInboundUndo(){
		$return=tableUndo('BagOrder');
        $this->ajaxReturn($return);
    }
	//////////////////////////////////////////////////////
}